package csce315.group10.androidapp;

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class DeleteMessageDialog extends Activity{
	/* private instance variables */
	public CustomApp appState;
	TopicCatalog topics_from_file = new TopicCatalog();
	Topic chosen_topic;
	String message_text;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.deletedialog);
        appState = ((CustomApp)getApplicationContext());
        Serializer serial = new Persister();
    	File sdcardFile = new File("/sdcard/Topics.xml");

    	try {
    		topics_from_file = serial.read(TopicCatalog.class, sdcardFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	/* find chosen topic */
  		for(int i = 0; i < topics_from_file.getTopics().size(); i++)
  		{
  			if(topics_from_file.getTopics().get(i).get_topic().equals(getIntent().getStringExtra("topic"))){
  				chosen_topic = topics_from_file.getTopics().get(i);
  				break;
  			}
  		}
    	/* get the message texts */
		String message_texts[] = new String[chosen_topic.getMessages().size()];
		for(int i = 0; i < chosen_topic.getMessages().size(); i++)
		{
			message_texts[i] = chosen_topic.getMessages().get(i).get_text();
		}
        Spinner spinner = (Spinner) findViewById(R.id.Delete_item_dropdown);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item,message_texts);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MessageSelectedListener());
    }
    
    public void okButtonHandler(View v){
    	Intent data = new Intent();
    	data.putExtra("message_text", message_text);
    	setResult(Activity.RESULT_OK, data);
    	finish();
    }
    /* class that monitors the message selector */
    public class MessageSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	message_text = parent.getItemAtPosition(pos).toString();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
}